import axios from './axios'
import qs from 'qs'
import {baseUrl} from '@/config/config'
var preUrl = baseUrl + '/dorm/api/app/stubed'
export const addStubed = (data) => {
    return new Promise((resolve, reject) => {
        axios({
            url: preUrl,
            method: 'post',
            data
        }).then(res => {
          resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}

export const getStubed = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: preUrl,
            method: 'get',
            params
        }).then(res => {
          resolve(res.data)
        })
    })
} 

export const editStubed = (data) => {
    return new Promise((resolve, reject) => {
        axios({
            url: preUrl + '/' + data.id,
            method: 'put',
            data
        }).then(res => {
          resolve(res.data)
        })
    })
} 

export const deleteStubed = (id) => {
    return new Promise((resolve, reject) => {
        axios({
            url: preUrl + '/' + id,
            method: 'delete',
            params: {id: id}
        }).then(res => {
          resolve(res.data)
        })
    })
}

export const getStubedById = (id) => {
    return new Promise((resolve, reject) => {
        axios({
            url: preUrl + '/' + id,
            method: 'get'
        }).then(res => {
          resolve(res.data)
        })
    })
}

// 上传图片
export const importStubed = (formData) => {
    return new Promise((resolve, reject) => {
        axios.post(
            preUrl + '/import/' + formData.schoolId, 
            formData, 
            { 
                headers: { 'Content-Type': 'multipart/form-data' }
            }
        ).then(res => {
            resolve(res.data) 
        }).catch(res => {
            reject(res)
        })      
    })
}

export const deleteMany = (list) => {
    return new Promise((resolve, reject) => {
        var str = '?'
        var arr = []
        for (var item of list) {
            arr.push('ids=' + item.id)
        }
        str += arr.join('&')
        axios({
            url: preUrl + '/batch' + str,
            method: 'delete'
        }).then(res => {
          resolve(res.data)
        })
    })
}
